package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"manage/models"
)

type WsterController struct {
	beego.Controller
}
type Msg struct {
	Code int
	Msg  string
	Data interface{}
}

var err error

func (c *WsterController) Login() {
	username := c.GetString("username")
	password := c.GetString("password")
	ss := models.Wster{
		Username: username,
		Password: username,
	}

	//err = models.DB.Debug().Create(&ss).Error
	if username == "" || password == "" {
		res := Msg{
			Code: 0,
			Msg:  "账号或密码不能为空",
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if err := ss.GetByUsername(); err != nil {
		res := Msg{
			Code: 0,
			Msg:  "系统错误",
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if ss.ID != 0 {
		res := Msg{
			Code: 0,
			Msg:  "账号已存在",
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if err := ss.Create(); err != nil {
		res := Msg{
			Code: 0,
			Msg:  "注册失败",
		}
		c.Data["json"] = res
		c.ServeJSON()
	} else {
		res := Msg{
			Code: 200,
			Msg:  "注册成功",
		}
		c.Data["json"] = res
		c.ServeJSON()
	}
	//ss.Username = username

}
